/*
 * *****************************************************************************
 * Copyright (C) 2014-2024 Dennis Sheirer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 * ****************************************************************************
 */

// Generated by jextract

package io.github.dsheirer.source.tuner.sdrplay.api.v3_15;

import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentAllocator;
import java.lang.foreign.SequenceLayout;
import java.lang.invoke.VarHandle;
import java.util.function.Consumer;

import static java.lang.foreign.MemoryLayout.PathElement.groupElement;
import static java.lang.foreign.MemoryLayout.PathElement.sequenceElement;
import static java.lang.foreign.ValueLayout.OfByte;
import static java.lang.foreign.ValueLayout.OfDouble;
import static java.lang.foreign.ValueLayout.OfInt;

/**
 * {@snippet lang=c :
 * struct {
 *     char SerNo[64];
 *     unsigned char hwVer;
 *     sdrplay_api_TunerSelectT tuner;
 *     sdrplay_api_RspDuoModeT rspDuoMode;
 *     unsigned char valid;
 *     double rspDuoSampleFreq;
 *     HANDLE dev;
 * }
 * }
 */
public class sdrplay_api_DeviceT {

    sdrplay_api_DeviceT() {
        // Should not be called directly
    }

    private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
        MemoryLayout.sequenceLayout(64, sdrplay_api_h.C_CHAR).withName("SerNo"),
        sdrplay_api_h.C_CHAR.withName("hwVer"),
        MemoryLayout.paddingLayout(3),
        sdrplay_api_h.C_INT.withName("tuner"),
        sdrplay_api_h.C_INT.withName("rspDuoMode"),
        sdrplay_api_h.C_CHAR.withName("valid"),
        MemoryLayout.paddingLayout(3),
        sdrplay_api_h.C_DOUBLE.withName("rspDuoSampleFreq"),
        sdrplay_api_h.C_POINTER.withName("dev")
    ).withName("$anon$145:9");

    /**
     * The layout of this struct
     */
    public static final GroupLayout layout() {
        return $LAYOUT;
    }

    private static final SequenceLayout SerNo$LAYOUT = (SequenceLayout)$LAYOUT.select(groupElement("SerNo"));

    /**
     * Layout for field:
     * {@snippet lang=c :
     * char SerNo[64]
     * }
     */
    public static final SequenceLayout SerNo$layout() {
        return SerNo$LAYOUT;
    }

    private static final long SerNo$OFFSET = 0;

    /**
     * Offset for field:
     * {@snippet lang=c :
     * char SerNo[64]
     * }
     */
    public static final long SerNo$offset() {
        return SerNo$OFFSET;
    }

    /**
     * Getter for field:
     * {@snippet lang=c :
     * char SerNo[64]
     * }
     */
    public static MemorySegment SerNo(MemorySegment struct) {
        return struct.asSlice(SerNo$OFFSET, SerNo$LAYOUT.byteSize());
    }

    /**
     * Setter for field:
     * {@snippet lang=c :
     * char SerNo[64]
     * }
     */
    public static void SerNo(MemorySegment struct, MemorySegment fieldValue) {
        MemorySegment.copy(fieldValue, 0L, struct, SerNo$OFFSET, SerNo$LAYOUT.byteSize());
    }

    private static long[] SerNo$DIMS = { 64 };

    /**
     * Dimensions for array field:
     * {@snippet lang=c :
     * char SerNo[64]
     * }
     */
    public static long[] SerNo$dimensions() {
        return SerNo$DIMS;
    }
    private static final VarHandle SerNo$ELEM_HANDLE = SerNo$LAYOUT.varHandle(sequenceElement());

    /**
     * Indexed getter for field:
     * {@snippet lang=c :
     * char SerNo[64]
     * }
     */
    public static byte SerNo(MemorySegment struct, long index0) {
        return (byte)SerNo$ELEM_HANDLE.get(struct, 0L, index0);
    }

    /**
     * Indexed setter for field:
     * {@snippet lang=c :
     * char SerNo[64]
     * }
     */
    public static void SerNo(MemorySegment struct, long index0, byte fieldValue) {
        SerNo$ELEM_HANDLE.set(struct, 0L, index0, fieldValue);
    }

    private static final OfByte hwVer$LAYOUT = (OfByte)$LAYOUT.select(groupElement("hwVer"));

    /**
     * Layout for field:
     * {@snippet lang=c :
     * unsigned char hwVer
     * }
     */
    public static final OfByte hwVer$layout() {
        return hwVer$LAYOUT;
    }

    private static final long hwVer$OFFSET = 64;

    /**
     * Offset for field:
     * {@snippet lang=c :
     * unsigned char hwVer
     * }
     */
    public static final long hwVer$offset() {
        return hwVer$OFFSET;
    }

    /**
     * Getter for field:
     * {@snippet lang=c :
     * unsigned char hwVer
     * }
     */
    public static byte hwVer(MemorySegment struct) {
        return struct.get(hwVer$LAYOUT, hwVer$OFFSET);
    }

    /**
     * Setter for field:
     * {@snippet lang=c :
     * unsigned char hwVer
     * }
     */
    public static void hwVer(MemorySegment struct, byte fieldValue) {
        struct.set(hwVer$LAYOUT, hwVer$OFFSET, fieldValue);
    }

    private static final OfInt tuner$LAYOUT = (OfInt)$LAYOUT.select(groupElement("tuner"));

    /**
     * Layout for field:
     * {@snippet lang=c :
     * sdrplay_api_TunerSelectT tuner
     * }
     */
    public static final OfInt tuner$layout() {
        return tuner$LAYOUT;
    }

    private static final long tuner$OFFSET = 68;

    /**
     * Offset for field:
     * {@snippet lang=c :
     * sdrplay_api_TunerSelectT tuner
     * }
     */
    public static final long tuner$offset() {
        return tuner$OFFSET;
    }

    /**
     * Getter for field:
     * {@snippet lang=c :
     * sdrplay_api_TunerSelectT tuner
     * }
     */
    public static int tuner(MemorySegment struct) {
        return struct.get(tuner$LAYOUT, tuner$OFFSET);
    }

    /**
     * Setter for field:
     * {@snippet lang=c :
     * sdrplay_api_TunerSelectT tuner
     * }
     */
    public static void tuner(MemorySegment struct, int fieldValue) {
        struct.set(tuner$LAYOUT, tuner$OFFSET, fieldValue);
    }

    private static final OfInt rspDuoMode$LAYOUT = (OfInt)$LAYOUT.select(groupElement("rspDuoMode"));

    /**
     * Layout for field:
     * {@snippet lang=c :
     * sdrplay_api_RspDuoModeT rspDuoMode
     * }
     */
    public static final OfInt rspDuoMode$layout() {
        return rspDuoMode$LAYOUT;
    }

    private static final long rspDuoMode$OFFSET = 72;

    /**
     * Offset for field:
     * {@snippet lang=c :
     * sdrplay_api_RspDuoModeT rspDuoMode
     * }
     */
    public static final long rspDuoMode$offset() {
        return rspDuoMode$OFFSET;
    }

    /**
     * Getter for field:
     * {@snippet lang=c :
     * sdrplay_api_RspDuoModeT rspDuoMode
     * }
     */
    public static int rspDuoMode(MemorySegment struct) {
        return struct.get(rspDuoMode$LAYOUT, rspDuoMode$OFFSET);
    }

    /**
     * Setter for field:
     * {@snippet lang=c :
     * sdrplay_api_RspDuoModeT rspDuoMode
     * }
     */
    public static void rspDuoMode(MemorySegment struct, int fieldValue) {
        struct.set(rspDuoMode$LAYOUT, rspDuoMode$OFFSET, fieldValue);
    }

    private static final OfByte valid$LAYOUT = (OfByte)$LAYOUT.select(groupElement("valid"));

    /**
     * Layout for field:
     * {@snippet lang=c :
     * unsigned char valid
     * }
     */
    public static final OfByte valid$layout() {
        return valid$LAYOUT;
    }

    private static final long valid$OFFSET = 76;

    /**
     * Offset for field:
     * {@snippet lang=c :
     * unsigned char valid
     * }
     */
    public static final long valid$offset() {
        return valid$OFFSET;
    }

    /**
     * Getter for field:
     * {@snippet lang=c :
     * unsigned char valid
     * }
     */
    public static byte valid(MemorySegment struct) {
        return struct.get(valid$LAYOUT, valid$OFFSET);
    }

    /**
     * Setter for field:
     * {@snippet lang=c :
     * unsigned char valid
     * }
     */
    public static void valid(MemorySegment struct, byte fieldValue) {
        struct.set(valid$LAYOUT, valid$OFFSET, fieldValue);
    }

    private static final OfDouble rspDuoSampleFreq$LAYOUT = (OfDouble)$LAYOUT.select(groupElement("rspDuoSampleFreq"));

    /**
     * Layout for field:
     * {@snippet lang=c :
     * double rspDuoSampleFreq
     * }
     */
    public static final OfDouble rspDuoSampleFreq$layout() {
        return rspDuoSampleFreq$LAYOUT;
    }

    private static final long rspDuoSampleFreq$OFFSET = 80;

    /**
     * Offset for field:
     * {@snippet lang=c :
     * double rspDuoSampleFreq
     * }
     */
    public static final long rspDuoSampleFreq$offset() {
        return rspDuoSampleFreq$OFFSET;
    }

    /**
     * Getter for field:
     * {@snippet lang=c :
     * double rspDuoSampleFreq
     * }
     */
    public static double rspDuoSampleFreq(MemorySegment struct) {
        return struct.get(rspDuoSampleFreq$LAYOUT, rspDuoSampleFreq$OFFSET);
    }

    /**
     * Setter for field:
     * {@snippet lang=c :
     * double rspDuoSampleFreq
     * }
     */
    public static void rspDuoSampleFreq(MemorySegment struct, double fieldValue) {
        struct.set(rspDuoSampleFreq$LAYOUT, rspDuoSampleFreq$OFFSET, fieldValue);
    }

    private static final AddressLayout dev$LAYOUT = (AddressLayout)$LAYOUT.select(groupElement("dev"));

    /**
     * Layout for field:
     * {@snippet lang=c :
     * HANDLE dev
     * }
     */
    public static final AddressLayout dev$layout() {
        return dev$LAYOUT;
    }

    private static final long dev$OFFSET = 88;

    /**
     * Offset for field:
     * {@snippet lang=c :
     * HANDLE dev
     * }
     */
    public static final long dev$offset() {
        return dev$OFFSET;
    }

    /**
     * Getter for field:
     * {@snippet lang=c :
     * HANDLE dev
     * }
     */
    public static MemorySegment dev(MemorySegment struct) {
        return struct.get(dev$LAYOUT, dev$OFFSET);
    }

    /**
     * Setter for field:
     * {@snippet lang=c :
     * HANDLE dev
     * }
     */
    public static void dev(MemorySegment struct, MemorySegment fieldValue) {
        struct.set(dev$LAYOUT, dev$OFFSET, fieldValue);
    }

    /**
     * Obtains a slice of {@code arrayParam} which selects the array element at {@code index}.
     * The returned segment has address {@code arrayParam.address() + index * layout().byteSize()}
     */
    public static MemorySegment asSlice(MemorySegment array, long index) {
        return array.asSlice(layout().byteSize() * index);
    }

    /**
     * The size (in bytes) of this struct
     */
    public static long sizeof() { return layout().byteSize(); }

    /**
     * Allocate a segment of size {@code layout().byteSize()} using {@code allocator}
     */
    public static MemorySegment allocate(SegmentAllocator allocator) {
        return allocator.allocate(layout());
    }

    /**
     * Allocate an array of size {@code elementCount} using {@code allocator}.
     * The returned segment has size {@code elementCount * layout().byteSize()}.
     */
    public static MemorySegment allocateArray(long elementCount, SegmentAllocator allocator) {
        return allocator.allocate(MemoryLayout.sequenceLayout(elementCount, layout()));
    }

    /**
     * Reinterprets {@code addr} using target {@code arena} and {@code cleanupAction} (if any).
     * The returned segment has size {@code layout().byteSize()}
     */
    public static MemorySegment reinterpret(MemorySegment addr, Arena arena, Consumer<MemorySegment> cleanup) {
        return reinterpret(addr, 1, arena, cleanup);
    }

    /**
     * Reinterprets {@code addr} using target {@code arena} and {@code cleanupAction} (if any).
     * The returned segment has size {@code elementCount * layout().byteSize()}
     */
    public static MemorySegment reinterpret(MemorySegment addr, long elementCount, Arena arena, Consumer<MemorySegment> cleanup) {
        return addr.reinterpret(layout().byteSize() * elementCount, arena, cleanup);
    }
}

